/*
===========================================================================
Copyright (C) 2013-2014 Avotu Briezhaudzetava

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

===========================================================================
*/

//
// includes
//
#include "gui_users.h"
#include "ui_gui_users.h"
#include <QSqlQuery>
#include "gui_main.h"
#include <QMessageBox>

/*
================
construct
================
*/
Gui_Users::Gui_Users( QWidget *parent ) : QDialog( parent ), ui( new Ui::Gui_Users ) {
    ui->setupUi( this );

    this->toggleWidgetVisibility( false );
    this->connect( this->ui->widgetAddEdit, SIGNAL( visibilityChanged( bool )), this, SLOT( toggleWidgetVisibility( bool )));

    // set up view
    this->listModel = new UserModel( this );
    this->ui->listUsers->setModel( this->listModel );
}

/*
================
destruct
================
*/
Gui_Users::~Gui_Users() {
    delete ui;
    delete this->listModel;
}

/*
================
toggleWidgetVisibility
================
*/
void Gui_Users::toggleWidgetVisibility( bool visible ) {
    if ( visible ) {
        this->ui->widgetAddEdit->setEnabled( true );
        this->ui->widgetAddEdit->show();
        this->ui->buttonAdd->setDisabled( true );
        this->ui->buttonEdit->setDisabled( true );
        this->ui->buttonRemove->setDisabled( true );
        this->ui->listUsers->setDisabled( true );
        this->clearItemState();
    } else {
        this->ui->widgetAddEdit->setDisabled( true );
        this->ui->widgetAddEdit->hide();
        this->ui->buttonAdd->setEnabled( true );
        this->ui->buttonEdit->setEnabled( true );
        this->ui->buttonRemove->setEnabled( true );
        this->ui->listUsers->setEnabled( true );
    }
}

/*
================
readItemState
================
*/
void Gui_Users::readItemState() {
    UserEntry *uPtr;

    if ( this->currentState() != Edit )
        return;

    uPtr = m.userForId( this->ui->listUsers->model()->data( this->ui->listUsers->currentIndex(), Qt::UserRole ).toInt());
    if ( uPtr == NULL )
        return;

    // set values
    this->ui->textName->setText( uPtr->name());
}

/*
================
clearItemState
================
*/
void Gui_Users::clearItemState() {
    this->ui->textName->clear();
}

/*
================
saveItemState
================
*/
void Gui_Users::saveItemState() {
    this->listModel->beginReset();

    if ( this->currentState() == Add ) {
        m.addUser( this->ui->textName->text());
    } else if ( this->currentState() == Edit ) {
        UserEntry *uPtr;

        uPtr = m.userForId( this->ui->listUsers->model()->data( this->ui->listUsers->currentIndex(), Qt::UserRole ).toInt());
        if ( uPtr == NULL )
            return;

        uPtr->setName( this->ui->textName->text());
    }

    this->listModel->endReset();
}

/*
================
toggleWidgetState
================
*/
void Gui_Users::toggleWidgetState( State state ) {
    this->m_state = state;

    switch ( state ) {
    case Add:
        this->toggleWidgetVisibility();
        this->ui->buttonAccept->setFocus();
        break;

    case Edit:
        this->toggleWidgetVisibility();
        this->readItemState();
        this->ui->buttonAccept->setFocus();
        break;

    case NoState:
    default:
        this->toggleWidgetVisibility( false );
        this->ui->buttonClose->setFocus();
        break;
    }
}

/*
================
buttonAdd->clicked
================
*/
void Gui_Users::on_buttonAdd_clicked() {
    this->toggleWidgetState( Add );
}

/*
================
buttonEdit->clicked
================
*/
void Gui_Users::on_buttonEdit_clicked() {
    this->toggleWidgetState( Edit );
}

/*
================
buttonRemove->clicked
================
*/
void Gui_Users::on_buttonRemove_clicked() {
#if 0
    QSqlQuery query;
    UserEntry *uPtr;

    uPtr = m.userForId( this->ui->listUsers->model()->data( this->ui->listUsers->currentIndex(), Qt::UserRole ).toInt());
    if ( uPtr == NULL )
        return;

    // begin reset
    this->listModel->beginReset();

    // remove from internal list
    m.usrerList.removeOne( uPtr );

    // remove from database
    query.exec( QString( "delete from users where id=%1" ).arg( uPtr->id()));

    // end reset
    this->listModel->endReset();

    // emit signal to main activity
    Gui_Main *guiPtr = qobject_cast<Gui_Main *>( this->parent());
    emit guiPtr->userListChanged();
#endif
}

/*
================
buttonAccept->clicked
================
*/
void Gui_Users::on_buttonAccept_clicked() {
    if ( this->currentState() == NoState )
        return;

    // failsafe - name
    if ( this->ui->textName->text().isEmpty()) {
        QMessageBox::warning( this, this->tr( "User management" ),
                                    this->tr( "Please specify user name" ), QMessageBox::Ok );
        return;
    }

    // all ok - proceed!
    this->saveItemState();
    this->toggleWidgetState();

    // emit signal to main activity
#if 0
    Gui_Main *guiPtr = qobject_cast<Gui_Main *>( this->parent());
    emit guiPtr->userListChanged();
#endif
}

/*
================
buttonClose->clicked
================
*/
void Gui_Users::on_buttonClose_clicked() {
    this->close();
}
